<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="充电站名称" prop="stationId">
        <el-select
          v-model="queryParams.stationId"
          placeholder="请选择充电站"
          style="width: 100%"
          filterable
          clearable
          disabled
        >
          <el-option
            v-for="item in chargeStationList"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          >
            <span style="float: left">{{ item.value }}</span>
            <span style="float: right; color: #4caf50; font-size: 13px">{{
              item.status
            }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="充电枪编码" prop="stakeCode">
        <el-input
          v-model="queryParams.stakeCode"
          placeholder="请输入充电枪编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="最大功率" prop="maxPower">
        <el-input v-model="queryParams.maxPower" placeholder="请输入最大功率" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="额定电流" prop="ratedCurrent">
        <el-input v-model="queryParams.ratedCurrent" placeholder="请输入额定电流" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="额定电压" prop="ratedVoltage">
        <el-input v-model="queryParams.ratedVoltage" placeholder="请输入额定电压" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="故障码" prop="faultCode">
        <el-input v-model="queryParams.faultCode" placeholder="请输入故障码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:stake:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:stake:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:stake:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:stake:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="stakeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="充电站名称" align="center" prop="stationName">
                <template slot-scope="scope">
                    {{ scope.row.params.stationName }}
                </template>
</el-table-column> -->
      <el-table-column label="充电枪编码" align="center" prop="stakeCode" />
      <el-table-column label="额定功率（kw）" align="center" prop="ratedPower">
        <template slot-scope="scope"> {{ scope.row.ratedPower }} </template>
      </el-table-column>
      <!-- <el-table-column label="最大功率" align="center" prop="maxPower">
        <template slot-scope="scope"> {{ scope.row.maxPower }}KW </template>
      </el-table-column>
      <el-table-column label="额定电流" align="center" prop="ratedCurrent">
        <template slot-scope="scope"> {{ scope.row.ratedCurrent }}A </template>
      </el-table-column>
      <el-table-column label="额定电压" align="center" prop="ratedVoltage">
        <template slot-scope="scope"> {{ scope.row.ratedVoltage }}V </template>
      </el-table-column> -->
      <!-- <el-table-column label="电流类型" align="center" prop="currentType" /> -->
      <el-table-column label="充电桩状态" align="center" prop="stakeStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.charge_stake_status"
            :value="scope.row.chargeStakeType"
            :show-overflow-tooltip="true"
          />
        </template>
      </el-table-column>
      <el-table-column label="离线续充" align="center" prop="isOnline">
        <template slot-scope="scope">
          {{ scope.row.isOnline?'在线':'离线' }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="充电速度" align="center" prop="stakeStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.charge_speed_type"
            :value="scope.row.chargeSpeedType"
          />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="故障码" align="center" prop="faultCode" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:stake:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:stake:remove']"
            style="color: red"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充电桩信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <!-- <el-form-item label="充电站" prop="stationId">
                    <el-select v-model="form.stationId" placeholder="请选择充电站" style="width: 100%;" filterable clearable>
                        <el-option v-for="item in chargeStationList" :key="item.id" :label="item.value"
                            :value="item.id">
                            <span style="float: left">{{ item.value }}</span>
                            <span style="float: right; color: #4CAF50; font-size: 13px">{{ item.status }}</span>
                        </el-option>
                    </el-select>
                </el-form-item> -->
        <el-row :gutter="20">
          <!-- <el-col :span="12"
            ><el-form-item label="充电速度" prop="chargeSpeedType">
              <el-select
                v-model="form.chargeSpeedType"
                placeholder="请选择充电速度（快充慢充）"
                filterable
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.charge_speed_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                >
                </el-option>
              </el-select> </el-form-item
          ></el-col> -->
          <el-col :span="12"
            ><el-form-item label="充电桩编码" prop="stakeCode">
              <el-input
                v-model="form.stakeCode"
                placeholder="请输入充电桩编码"
              /> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="电桩类型" prop="chargeStakeType">
              <el-select
                v-model="form.chargeStakeType"
                placeholder="请选择电桩类型"
                filterable
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.charge_stake_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                >
                </el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="额定功率(kw)" prop="ratedPower">
              <el-input-number
                v-model="form.ratedPower"
                placeholder="请输入额定功率"
                style="width: 100%"
              >
                <template slot="append"><span>KW</span></template>
              </el-input-number>
            </el-form-item></el-col
          >
          <el-col :span="12"
            ><el-form-item label="离线续充" prop="isOnline">
              <el-radio-group v-model="form.isOnline">
                <el-radio :label="'1'" :value="'1'">支持</el-radio>
                <el-radio :label="'0'" :value="'0'">不支持</el-radio>
              </el-radio-group>
            </el-form-item></el-col
          >

          <!-- <el-col :span="12"
            ><el-form-item label="充电枪编号" prop="gunNum">
              <el-radio-group v-model="form.gunNum">
                <el-radio :label="1" :value="1">A</el-radio>
                <el-radio :label="2" :value="2">B</el-radio>
              </el-radio-group>
            </el-form-item></el-col
          >
          <el-col :span="12"
            ><el-form-item label="充电枪状态" prop="gunStatus">
              <el-radio-group v-model="form.gunStatus">
                <el-radio :label="0" :value="0">待机</el-radio>
                <el-radio :label="1" :value="1">充电</el-radio>
              </el-radio-group>
            </el-form-item></el-col
          > -->
          <!-- <el-col :span="12"
            ><el-form-item label="最大功率(kw)" prop="maxPower">
              <el-input-number
                v-model="form.maxPower"
                placeholder="请输入最大功率"
                style="width: 100%"
              >
                <template slot="append"><span>KW</span></template>
              </el-input-number>
            </el-form-item></el-col
          > -->

          <!-- <el-col :span="12"
            ><el-form-item label="额定电流(A)" prop="ratedCurrent">
              <el-input-number
                v-model="form.ratedCurrent"
                placeholder="请输入额定电流"
                style="width: 100%"
              >
                <template slot="append"><span>A</span></template>
              </el-input-number>
            </el-form-item></el-col
          >
          <el-col :span="12"
            ><el-form-item label="额定电压(V)" prop="ratedVoltage">
              <el-input-number
                v-model="form.ratedVoltage"
                placeholder="请输入额定电压"
                style="width: 100%"
              >
                <template slot="append"><span>V</span></template>
              </el-input-number>
            </el-form-item></el-col
          > -->
        </el-row>

        <!-- <el-form-item label="故障码" prop="faultCode">
          <el-input v-model="form.faultCode" placeholder="请输入故障码" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStake,
  getStake,
  delStake,
  addStake,
  updateStake,
} from "@/api/business/stake";
import { selectChargeStationToSelect } from "@/api/business/chargeStation";

export default {
  name: "Stake",
  dicts: ["charge_speed_type", "charge_stake_status", "charge_stake_type"],
  props: {
    stationId: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充电桩信息表格数据
      stakeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationId: null,
        stakeCode: null,
        maxPower: null,
        ratedCurrent: null,
        ratedVoltage: null,
        currentType: null,
        stakeStatus: null,
        faultCode: null,
      },
      // 表单参数
      form: { isOnline: '0' },
      // 表单校验
      rules: {
        stationId: [
          { required: true, message: "充电站ID不能为空", trigger: "blur" },
        ],
        stakeCode: [
          { required: true, message: "充电枪编码不能为空", trigger: "blur" },
        ],
        gunNum: [
          { required: true, message: "充电枪编号不能为空", trigger: "blur" },
        ],
        gunStatus: [
          { required: true, message: "充电枪状态不能为空", trigger: "blur" },
        ],

        maxPower: [
          { required: true, message: "最大功率不能为空", trigger: "blur" },
        ],
        ratedPower: [
          { required: true, message: "额定功率不能为空", trigger: "blur" },
        ],
        chargeStakeType: [
          { required: true, message: "请选择电桩类型", trigger: "change" },
        ],
        isOnline: [
          { required: true, message: "请选择续充类型", trigger: "change" },
        ],
        ratedCurrent: [
          { required: true, message: "额定电流不能为空", trigger: "blur" },
        ],
        ratedVoltage: [
          { required: true, message: "额定电压不能为空", trigger: "blur" },
        ],
        currentType: [
          { required: true, message: "电流类型不能为空", trigger: "change" },
        ],
        stakeStatus: [
          { required: true, message: "充电桩状态不能为空", trigger: "change" },
        ],
        chargeSpeedType: [
          { required: true, message: "充电速度不能为空", trigger: "change" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
      },
      // 充电站列表
      chargeStationList: [],
    };
  },
  created() {
    this.queryParams.stationId = this.stationId;
    this.getList();
    this.getChargeStationList();
  },
  methods: {
    /** 查询充电桩信息列表 */
    getList() {
      this.loading = true;
      listStake(this.queryParams).then((response) => {
        this.stakeList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(123)
      });
    },
    // 查询充电桩下拉列表信息
    getChargeStationList() {
      selectChargeStationToSelect().then((response) => {
        if (response.code == 200) {
          this.chargeStationList = response.data;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stakeId: null,
        stationId: null,
        stakeCode: null,
        maxPower: null,
        ratedPower: null,
        ratedCurrent: null,
        ratedVoltage: null,
        currentType: null,
        stakeStatus: null,
        faultCode: null,
        updateTime: null,
        createTime: null,
        chargeStakeType:null,
        isOnline: '0',
        gunNum: 1,
        gunStatus: 0,
        chargeSpeedType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.stakeId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.stationId = this.stationId;
      this.title = "添加充电桩信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const stakeId = row.stakeId || this.ids;
      getStake(stakeId).then((response) => {
        this.form = response.data;
        // this.form.chargeSpeedType = this.form.chargeSpeedType + "";
        this.form.chargeStakeType = this.form.chargeStakeType+''
        // this.form.stakeStatus = this.form.stakeStatus + "";
        this.form.stationId = this.stationId;
        this.open = true;
        this.title = "修改充电桩信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.stakeId != null) {
            updateStake(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStake(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const stakeIds = row.stakeId || this.ids;
      this.$modal
        .confirm('是否确认删除充电桩信息编号为"' + stakeIds + '"的数据项？')
        .then(function () {
          return delStake(stakeIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/stake/export",
        {
          ...this.queryParams,
        },
        `stake_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
